package com.hancher.plugins.hrasmlog;

import com.android.build.api.transform.DirectoryInput;
import com.android.build.api.transform.Format;
import com.android.build.api.transform.JarInput;
import com.android.build.api.transform.QualifiedContent;
import com.android.build.api.transform.Transform;
import com.android.build.api.transform.TransformException;
import com.android.build.api.transform.TransformInput;
import com.android.build.api.transform.TransformInvocation;
import com.android.build.api.transform.TransformOutputProvider;
import com.android.build.gradle.internal.pipeline.TransformManager;
import com.android.utils.FileUtils;

import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Opcodes;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Collection;
import java.util.Set;

class HrAsmLogPluginProject extends Transform implements Plugin<Project> {
    // region 插件生命周期
    @Override
    public void apply(Project project) {
        System.out.println("========HrAsmLogPluginProject==========apply");
    }
    //endregion

    // region transform 配置
    @Override
    public String getName() {
        return "HrAsmLogPluginProject";
    }

    @Override
    public Set<QualifiedContent.ContentType> getInputTypes() {
        //要处理的数据类型是java class文件，并且包含jar文件
        return TransformManager.CONTENT_CLASS;
    }

    @Override
    public Set<? super QualifiedContent.Scope> getScopes() {
        //整个项目工程+外部library库
        return TransformManager.SCOPE_FULL_PROJECT;
    }

    @Override
    public boolean isIncremental() {
        return false;
    }

    @Override
    public void transform(TransformInvocation transformInvocation) throws TransformException, InterruptedException, IOException {
        System.out.println("========HrAsmLogPluginProject==========transform");
//        super.transform(transformInvocation);
        Collection<TransformInput> inputs = transformInvocation.getInputs();
        TransformOutputProvider out = transformInvocation.getOutputProvider();
        // 输入 遍历
        for (TransformInput transformInput : inputs) {
            // 项目目录 遍历
            for (DirectoryInput directoryInput : transformInput.getDirectoryInputs()) {
                if (directoryInput.getFile().isDirectory()) {
                    // 目录下的文件
                    for (File file : FileUtils.getAllFiles(directoryInput.getFile())) {
                        String name = file.getName();
                        if (name.endsWith(".class") && !name.equals("R.class") && !name.startsWith("R$") && !name.equals("BuildConfig.class")) {
                            // 找到需要的.class 进行读写操作
                            FileInputStream fis = new FileInputStream(file);
                            ClassReader cr = new ClassReader(fis);
                            ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_MAXS);
                            HrAsmLogClassVisitor visitor = new HrAsmLogClassVisitor(Opcodes.ASM7, cw);
                            cr.accept(visitor, ClassReader.EXPAND_FRAMES);

                            byte[] bytes = cw.toByteArray();
                            FileOutputStream fos = new FileOutputStream(file);
                            fos.write(bytes);
                            fos.close();
                        }
                    }
                }
                File dest = out.getContentLocation(directoryInput.getName(), directoryInput.getContentTypes(),
                        directoryInput.getScopes(), Format.DIRECTORY);
                FileUtils.copyDirectoryToDirectory(directoryInput.getFile(), dest);
            }
            // jar包
            for (JarInput it : transformInput.getJarInputs()) {
                File dest = out.getContentLocation(it.getName(), it.getContentTypes(), it.getScopes(), Format.JAR);
                FileUtils.copyFile(it.getFile(), dest);
            }
        }
    }
    // endregion

}